<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="__STATIC__/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="__STATIC__/css/font.css">
    <link rel="stylesheet" href="__STATIC__/css/xadmin.css">
    <link rel="stylesheet" href="__STATIC__/css/one.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="__STATIC__/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="__STATIC__/js/xadmin.js"></script>

    <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.js"></script>

    <link rel="stylesheet" href="__STATIC__/editor/themes/default/default.css" />
    <style type="text/css">
        .coach {
            line-height: 30px!important;
        }
        .jiaolian{
            text-align: center;
            width: 700px !important;
            display: inline-block;
        }
        .layui-form-item{
            margin-bottom: 20px;
        }
    </style>
    <script type="text/javascript" src="__STATIC__/editor/kindeditor.js"></script>
    <script src="__STATIC__/editor/lang/zh_CN.js"></script>
</head>
<body>
<form class="layui-form" id="form1">
    <input type="hidden" name="id" value="{$data1.id}"/>
    <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
        <ul class="layui-tab-title">
            <li class="layui-this">基本资料</li>
            <li {if $data1['typeid']==1} style='display:none;'{/if} class="cs">参数</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-row layui-show">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red"></span>预约练车类型
                    </label>
                    <div class="layui-input-inline" >
                        <input {if $data1['typeid'] eq 0}checked{/if} type="radio" name="type" lay-filter="cc" value="0" title="周一到周六">
                        <input {if $data1['typeid'] eq 1}checked{/if}  type="radio" name="type" lay-filter="cc" value="1" title="周天">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red"></span>驾照类型
                    </label>
                    <div class="layui-input-inline">
                        {volist name='driver' id='v' key='k'}
                        <input type="radio" name="driver" {if condition="$data1['jzid'] == $v['id']"}checked{/if} value="{$v.id}" title="{$v.name}"/>
                        {/volist}
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red"></span>科目类型
                    </label>
                    <div class="layui-input-inline">
                        {volist name='kemu' id='v' key='k'}
                        <input {if condition="$data1['kmid']==$v['id']"}checked{/if} type="radio" name="kemu" title="{$v.class_name}" value="{$v.id}"/>
                        {/volist}
                    </div>
                </div>
                <div {if condition='$data1["typeid"] eq 0'}style="display:none;"{/if} class="layui-form-item jiages">
                    <label class="layui-form-label">
                        <span class="x-red"></span>周天价格
                    </label>
                    <div class="layui-input-inline">
                        <input class="layui-input" value="{$data1.price}" placeholder="周天价格" name="price"  lay-key="1">
                    </div>
                </div>

            </div>
                <div class="layui-tab-item can">
                    <div class="layui-col-md7">
                        <i class="layui-form-label layui-icon layui-icon-add-1" id="jia">添加日期</i>
                        {volist name='data' id='v' key='k'}
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    <span class="x-red"></span>日期
                                </label>
                                <div class="layui-input-inline">
                                    <input class="layui-input datess"  value="{$v.date}" placeholder="午休时间" name="rest_time[]" id="rest" lay-key="1">
                                </div>
                                {if condition='$k gt 1'}
                                <i class="layui-icon layui-icon-close coachclose">删除日期</i>
                                {/if}
                                <div class="jiaolian layui-input-inline">
                                    <label class="layui-form-label"><span class="x-red"></span>教练</label>
                                    <div class="coachsss layui-input-inline" style="width:390px;">
                                    {volist name='v["coach"]' id='vv'}
                                        <input {if condition="$vv['status']==0"}disabled{/if} {if condition="strstr($v['coachid'],''.$vv['id'])"}checked{/if} name="coach[]" lay-skin="primary" type="checkbox" title="{$vv.name}" value="{$vv.id}">
                                    {/volist}
                                     </div>
                                    <div style="width:1200px;clear:both;">
                                        {volist name='v["child"]' id='vv' key='kk'}
                                        {if condition='$kk==1'}
                                            <label class="layui-form-label">时间</label>
                                        {/if}
                                        <div class="shijian layui-input-inline">
                                            <input class="layui-input layui-input-inline" value="{$vv.time}" placeholder="午休时间" name="shijian[]">
                                            <input class="layui-input layui-input-inline" value="{$vv.price}" placeholder="价格" name="jiage[]">
                                            <input class="layui-input layui-input-inline" value="{$vv.count}" placeholder="库存" name="kc[]">
                                            <span class="layui-icon layui-icon-add-1 shijianopen">增加时间</span>
                                            {if condition='$kk egt 2'}
                                            &nbsp;&nbsp;<i class=" layui-icon-close layui-icon shijianclose">删除时间</i>
                                            {/if}
                                        </div>
                                        {/volist}
                                    </div>
                                  </div>
                             </div>
                        {/volist}
               <div class="layui-btn layui-btn-warm" id="sc">生成</div>
        </div>
        </div>
        <div class="layui-form-item">
            <label  class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                确认保存
            </button>
        </div>
    </div>
</form>
<script type="text/javascript">
    layui.use(['form', 'layer','element','upload','laydate'], function () {
        var str='';
        var shijian='';
        $ = layui.jquery;
        var form = layui.form
            , element = layui.element
            , layer = layui.layer
            ,upload = layui.upload
            ,laydate = layui.laydate;
        $(document).on('change', '.datess', function() {
            $('.datess').css('border','0px');
            var str='';
            var date=$(this).val();
            if($(this).parent().next('.jiaolian').attr('class')==undefined){
                $(this).parent().next().next('.jiaolian').find('.coachsss').empty();
            }else{
                $(this).parent().next('.jiaolian').find('.coachsss').empty();
            }
            $.ajax({
                type:"POST",
                url:'{:url("yuyue/getcoach")}',
                async: false,
                data:{'date':date},
                success:function(re){
                    $.each(re.data, function (k,v) {
                        if(v.status==0){
                            str += '<input disabled name="coach[]" lay-skin="primary" type="checkbox" title="' + v.name + '" value="' + v.id + '">';
                        }else{
                            str += '<input name="coach[]" lay-skin="primary" type="checkbox" title="' + v.name + '" value="' + v.id + '">';
                        }
                    });
                }
            });
            if($(this).parent().next('.jiaolian').attr('class')==undefined){
                $(this).parent().next().next('.jiaolian').find('.coachsss').append(str);
            }else{
                $(this).parent().next('.jiaolian').find('.coachsss').append(str);
            }
            form.render();
        });
        form.on('radio(cc)',function(data){
            if($(this).val()==1){
                $('.jiages').show('slow');
                $('.cs').hide();
            }else{
                $('.jiages').hide('slow');
                $('.cs').show();
            }
        })
        //增加日期
        $(document).on('click','#jia',function(data){
            var index=$('#jia').nextAll('.layui-form-item').length-1;
            var datenext=null;
            if(index==-1){
              datenext=(new Date()).getTime()+24*60*60*1000;

            }else{
                var date=$('.can .layui-form-item').eq(index).find('.layui-input').val();
                datenext=(new Date(date)).getTime()+24*60*60*1000;
            }
            var month=(new Date(datenext)).getMonth()+1;
            if(month<10){
                month='0'+month;
            }
            var daytime=(new Date(datenext)).getFullYear()+'-'+month+'-'+(new Date(datenext)).getDate();
            var con='<div class="layui-form-item">\n' +
                '                            <label class="layui-form-label">\n' +
                '                                <span class="x-red"></span>日期\n' +
                '                            </label>\n' +
                '                            <div class="layui-input-inline">\n' +
                '                                <input class="layui-input datess" value="'+daytime+'" placeholder="午休时间" name="rest_time[]" class="rest" lay-key="1">\n' +
                '                            </div>\n' +
                '<i class="layui-icon layui-icon-add-1 coach">添加教练和时间</i><i class="layui-icon layui-icon-close coachclose">删除日期</i>'+
                '                        </div>'
            if(index==-1){
                $('#jia').after(con);
            }else{
                $('.can .layui-form-item').eq(index).after(con);
            }
        })
        //增加教练，时间和库存
        $(document).on('click','.coach',function(data){
            var date=$(this).prev().find('input[name^=rest_time]').val();
                shijian='<div class="shijian layui-input-inline">' +
                    '<input class="layui-input layui-input-inline" value="08:20-09:00" placeholder="午休时间" name="shijian[]">' +
                    '<input class="layui-input layui-input-inline" value="" placeholder="价格" name="jiage[]">' +
                    '<input class="layui-input layui-input-inline" value="" placeholder="库存" name="kc[]">' +
                    '<span class="layui-icon layui-icon-add-1 shijianopen">增加时间</span>' +
                    '</div>';
                $.ajax({
                    url:'{:url("yuyue/getcoach")}',
                    data:{'date':date},
                    async: false,
                    success:function (re) {
                        $.each(re.data, function (k, v) {
                            str += '<input name="coach[]" lay-skin="primary" type="checkbox" title="' + v.name + '" value="' + v.id + '">';
                        });
                    },
                });
                var con='<div class="jiaolian layui-input-inline"><label class="layui-form-label">\n' +
                    '                                <span class="x-red"></span>教练\n' +
                    '                            </label><div class="coachsss layui-input-inline" style="width:390px;">\n'
                                                    +str+
                    '                            </div>' +
                    '<div style="clear:both;"><label class="layui-form-label">时间</label>'
                    +shijian+
                    '</div></div>';
                $(this).after(con);
                $(this).remove();
            form.render();
        });
        //增加时间，价格和kc
        $(document).on('click','.shijianopen',function(data){
            var shijian='<div class="shijian layui-input-inline">' +
                '<input class="layui-input layui-input-inline" value="08:20-09:00" placeholder="午休时间" name="shijian[]">' +
                '<input class="layui-input layui-input-inline" value="" placeholder="价格" name="jiage[]">' +
                '<input class="layui-input layui-input-inline" value="" placeholder="库存" name="kc[]">' +
                '<i class=" layui-icon-add-1 layui-icon shijianopen">增加时间</i>&nbsp;&nbsp;<i class=" layui-icon-close layui-icon shijianclose">删除时间</i>' +
                '</div>';
            $(this).parent().after(shijian);
        });
        $(document).on('click','.shijianclose',function(data){
            $(this).parent().remove();
        });
        var date=[];
        var jiaolian=[];
        var peizhi=[];
        var temps=[[]]
        $(document).on('click','#sc',function(data){
            var ary=[];
            var flag=true;
            $.each($('.datess'),function(k,v){
                ary[ary.length]=$(v).val();
            })
            console.log(ary);
            var s = ary.join(",") +",";
            for(var i = 0; i < ary.length; i++)
            {
                if(s.replace(ary[i] + ",", "").indexOf(ary[i] +",") > -1)
                {
                   $('.datess').eq(i).css('border','3px solid red');
                   flag=false;
                }
            }
            if(!flag){
                layer.msg('日期重复');
                return;
            }
            date=[];
            jiaolian=[];
            peizhi=[];
            $.each($('#jia').nextAll('.layui-form-item'),function(k,v){
                temps=[];
                var datetime=$(v).find($('input[name^="rest"]')).val();
                var jiaolianids='';
                $.each($(v).find($('input[name^="coach"]:checked')),function(kk,vv){
                    jiaolianids+=$(vv).val()+',';
                });
                var temp='';
                $.each($(v).find($('.shijian')),function(kk,vv){
                     var times=$(vv).find($('input[name^="shijian"]')).val();
                     var jiage=$(vv).find($('input[name^="jiage"]')).val();
                     var kc=$(vv).find($('input[name^="kc"]')).val();
                     temp+=times+'='+jiage+'='+kc+'//';
                     peizhi[date.length]=temp;
                });
                jiaolian[date.length]=jiaolianids;
                date[date.length]=datetime;
            })
            if(date.length==0||peizhi.length==0){
                layer.msg('生成失败添加时间。。',{icon:2,time:3000});
            }else{
                layer.msg('生成成功。。确认保存',{icon:1,time:3000});
            }
        });
        $(document).on('click','.coachclose',function(data){
            $(this).parent().remove();
        });
        //监听提交
        form.on('submit(add)', function (data) {
            if(data.field.type==0){
                if(date.length==0){
                    layer.msg('点击生成')
                    return false;
                }
                if(date.length==0||peizhi.length==0){
                    layer.msg('生成失败添加时间。。',{icon:2,time:3000});
                    return false;
                }
            }
            $.post(
                '{:url("yuyue/yuyueEditForm")}',
                {'data':data.field,'date':date,'jiaolian':jiaolian,'peizhi':peizhi},
                function (res) {
                if (res.code == 1){
                    layer.msg(res.msg, {icon: 6,time:1000}, function () {
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    });
                }else {
                    layer.msg(res.msg, {icon: 5,time:1000});
                }
            });
            return false;
        });
    })

</script>
</body>

</html>
